package com.angenin.springcloud.controller;

import com.angenin.springcloud.entities.CommonResult;
import com.angenin.springcloud.entities.Payment;
import com.angenin.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * controller 调 servers  调  dao
 */
@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;
    //@value 提供从配置文件中获取到对应的端口号
    @Value("${server.port}")
    private String serverPort;
    /**
     * CommonResult 因为这个要前端返回，所以不能写Payment，应该写成CommonResult
     *  @PostMapping  插入用post
     *  @GetMapping   查询用get
     *  @PathVariable    绑定的占位符
     */

    @PostMapping(value = "/payment/create")
    // @RequestBody  这个注解的作用就是让插入的数据不为null
    public CommonResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("******插入的数据为：" + payment);
        log.info("******插入结果：" + result);

        //返回给前端
        if(result>0){
            return new CommonResult(200,"插入数据库成功,serverPort:"+serverPort,result);
        }else{
            return new CommonResult(444, "插入数据库失败",null);
        }
    }
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("******查询结果：" + payment);

        //返回给前端
        if(payment != null){
            return new CommonResult(200,"查询数据库成功,serverPort:"+serverPort,payment);
        }else{
            return new CommonResult(444, "查询数据库失败",null);
        }
    }
}
